﻿using System;
using System.Linq;
using System.Web.Security;
using WebTech.Coursework02.Models;

namespace WebTech.Coursework02.Utilities.HRProviders
{
    public class HRRoleProvider : RoleProvider
    {
        public override string[] GetRolesForUser(string username)
        {          
            // connect to the role provider and match the username based on the roles.
            using (ExtractionEntities ex = new ExtractionEntities())
            {
               
                    // check if the current user is an employee or he is the admin user
                int empNo = Convert.ToInt32(username);
                var currentUser = ex.employees.FirstOrDefault(x => x.emp_no == empNo);

                    return (currentUser == null) ? null : currentUser.application_roles.Select(x => x.role_name).ToArray();

            }
        }

       

        public override void AddUsersToRoles(string[] empIds, string[] roleNames)
        {
            if ((empIds != null) && (roleNames != null))
            {
                if ((empIds.Length > 0) && (roleNames.Length > 0))
                {
                    for (int i = 0; i < empIds.Length; i++)
                    {
                        for (int j = 0; j < roleNames.Length; j++)
                        {
                            using (ExtractionEntities ex = new ExtractionEntities())
                            {
                                int empId = Convert.ToInt32(empIds[i]);
                                string role = roleNames[i];
                                var currentUser = ex.employees.FirstOrDefault(x => x.emp_no == empId);
                                var newRole = ex.application_roles.First(x => x.role_name == role);
                                currentUser.application_roles.Add(newRole);
                                ex.SaveChanges();
                            }
                        }
                    }
                  
                }
                
            }
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            //throw new NotImplementedException();
            if( (!string.IsNullOrEmpty(username)) &&  (!string.IsNullOrEmpty(roleName)))
            {
                return true;
            }

            return false;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}